#include <iostream>
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (!head || !head->next)return head;
        ListNode* dummp = new ListNode(0);
        dummp->next = head;
        ListNode* prev = dummp;
        while (head) {
            ListNode* first = prev->next;
            if (!first)break;
            ListNode* second = first->next;
            if (!second)break;
            first->next = second->next;
            second->next = first;
            prev->next = second;
            prev = first;
        }
        return dummp->next;
    }
};
int main() {
    ListNode* AA = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
    Solution sol;
    ListNode* l1 = sol.swapPairs(AA);
    while (l1) {
        cout << l1->val;
        l1 = l1->next;
    }
    return 0;
}